// pages/notedetail/notedetail.js
import request from '../../utils/request'
import {$toast} from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notedetail:null,
    editorCtx: '',
    getrevisebtn:''
  },
  onEditorReady(e) {
    let that = this;
    wx.createSelectorQuery().select('#editor').context((res) => {
      that.editorCtx = res.context;
      that.editorCtx.setContents({
        html: '数据请求中'
      })
    }).exec()
  },
  getContent() {
    let that = this;
    that.editorCtx.getContents({
      success: (e)=> {
        that.content = e.html;
      }
    });
  },
  getrevisebtn(e){
    console.log(e.detail.html);
    this.setData({
      getrevisebtn:e.detail.html
    })
  },
  revisebtn(){
    console.log(this.data.getrevisebtn);
     //提交给数据库
     request('/diary/revisebtn','post',{
      note_content:this.data.getrevisebtn,
      title:this.data.notedetail.title,
      head_img:this.data.notedetail.head_img,
      note_type:this.data.notedetail.note_type,
      nickname:this.data.notedetail.nickname,
      userid:this.data.notedetail.useId
    }).then(res =>{
      console.log(res.data.mess);
      $toast(res.data.mess)
      setTimeout(()=>{
        wx.navigateBack()
      },1000)
      
    })
  },
  deletebtn(){
    
    request('/diary/deletebtn','post',{
      note_content:this.data.notedetail.note_content,
      title:this.data.notedetail.title,
      head_img:this.data.notedetail.head_img,
      note_type:this.data.notedetail.note_type,
      nickname:this.data.notedetail.nickname,
      userid:this.data.notedetail.useId
    }).then(res =>{
      console.log(res.data.mess);
      $toast(res.data.mess)
      setTimeout(()=>{
        wx.navigateBack()
      },1000)
      
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.id);
    let id = options.id
    request('/diary/findNoteDetailById','get',{
      id:id
    }).then((res)=>{
      console.log(res.data.data);
      let obj = res.data.data
      this.setData({
        notedetail:obj[0]
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let that = this
    setTimeout(() =>{
      console.log(this.editorCtx);
      that.editorCtx.setContents({
        html: this.data.notedetail.note_content
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})